//
//  TSConvertSectionHeaderView.swift
//  TSProfitProject
//
//  Created by TmmmS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

enum TSConvertType {
   case convertCash
   case convertCoin
   case convertGain
   case convertGainToCash
}

protocol TSConvertSectionHeaderViewDelegate : NSObjectProtocol {
    func converToCoin(money:String, convertType:TSConvertType)
}

class TSConvertSectionHeaderView: UIView {

    weak var delegate : TSConvertSectionHeaderViewDelegate?
    private var typeField : UITextField!
    private var cashField : UITextField!
    private var currentConvertType = TSConvertType.convertCoin
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func checkCommitCondition() -> Bool {
        
        let money = cashField.text?.removeAllSapce as NSString?
        guard money?.doubleValue != 0 else {
            CBToast.showToastAction(message: "请输入金额")
            return false
        }
        
        return true
    }
    
    @objc private func tapGesture(gesture:UITapGestureRecognizer) {
        
        let alertVC = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
        let action1 = UIAlertAction.init(title: "中心钱包转金币", style: .default) { (UIAlertAction) in
            self.typeField.placeholder = "中心钱包转金币"
            self.currentConvertType = TSConvertType.convertCoin
        }
        let action2 = UIAlertAction.init(title: "金币钱包转中心", style: .default) { (UIAlertAction) in
            self.typeField.placeholder = "金币钱包转中心"
            self.currentConvertType = TSConvertType.convertCash
        }
        let action3 = UIAlertAction.init(title: "收益钱包转盈利", style: .default) { (UIAlertAction) in
            self.typeField.placeholder = "收益钱包转盈利"
            self.currentConvertType = TSConvertType.convertGain
        }
        let action4 = UIAlertAction.init(title: "盈利钱包转中心", style: .default) { (UIAlertAction) in
            self.typeField.placeholder = "盈利钱包转中心"
            self.currentConvertType = TSConvertType.convertGainToCash
        }
        let action5 = UIAlertAction.init(title: "取消", style: .destructive, handler: nil)
        
        alertVC.addAction(action1)
        alertVC.addAction(action2)
        alertVC.addAction(action3)
        alertVC.addAction(action4)
        alertVC.addAction(action5)
        UIViewController.currentViewController().present(alertVC, animated: true, completion: nil)
    }
        
    @objc private func commitAction() {
        
        if checkCommitCondition() {
            cashField.resignFirstResponder()
            if self.delegate != nil {
                self.delegate?.converToCoin(money: cashField.text!.removeAllSapce, convertType: currentConvertType)
            }
        }
    }
    
    private func configViews() {
        
        let typeContainer = UIView.init()
        typeContainer.backgroundColor = WHITE_COLOR
        typeContainer.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(tapGesture(gesture:))))
        self.addSubview(typeContainer)
        typeContainer.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(50)
        }
        
        let typeLabel = UILabel.init()
        typeLabel.text = "转换类型"
        typeLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        typeLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        typeContainer.addSubview(typeLabel)
        typeLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(15)
            make.width.equalTo(60)
        }
        
        let icon = UIImageView.init(image: UIImage(named: "account_conversion_arrow"))
        typeContainer.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-15)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        typeField = createInputField(placeholder: "中心钱包转金币")
        typeField.isEnabled = false
        typeContainer.addSubview(typeField)
        typeField.snp.makeConstraints { (make) in
            make.left.equalTo(typeLabel.snp.right).offset(15)
            make.right.equalTo(icon.snp.left).offset(-15)
            make.top.bottom.equalToSuperview()
        }
        
        let line1 = drawLine()
        typeContainer.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.bottom.equalToSuperview()
            make.size.equalTo(CGSize(width: SCREEN_WIDTH-2*12, height: 1))
        }
        
        let cashContainer = UIView.init()
        cashContainer.backgroundColor = WHITE_COLOR
        self.addSubview(cashContainer)
        cashContainer.snp.makeConstraints { (make) in
            make.top.equalTo(typeContainer.snp.bottom)
            make.left.right.height.equalTo(typeContainer)
        }
        
        let cashLabel = UILabel.init()
        cashLabel.text = "转换金额"
        cashLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        cashLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        cashContainer.addSubview(cashLabel)
        cashLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(15)
            make.width.equalTo(typeLabel)
        }
        
        cashField = createInputField(placeholder: "输入金额")
        cashField.keyboardType = .decimalPad
        cashContainer.addSubview(cashField)
        cashField.snp.makeConstraints { (make) in
            make.left.equalTo(cashLabel.snp.right).offset(15)
            make.right.equalTo(-15)
            make.top.bottom.equalToSuperview()
//            make.right.equalTo(cashLabel.snp.left).offset(-10)
        }
        
        let line2 = drawLine()
        cashContainer.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.bottom.equalToSuperview()
            make.size.equalTo(CGSize(width: SCREEN_WIDTH-2*12, height: 1))
        }
        
        let commitBtn = UIButton.init(type: .system)
        commitBtn.setTitle("提交", for: .normal)
        commitBtn.setTitleColor(WHITE_COLOR, for: .normal)
        commitBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        commitBtn.backgroundColor = BASE_RED_COLOR
        commitBtn.layer.cornerRadius = 24
        commitBtn.layer.masksToBounds = true
        commitBtn.addTarget(self, action: #selector(commitAction), for: .touchUpInside)
        self.addSubview(commitBtn)
        commitBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(cashContainer.snp.bottom).offset(30)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
        }
        
    }
    
    func createInputField(placeholder:String) -> UITextField {
        
        let string = placeholder
        let attributeStr = NSMutableAttributedString.init(string: string)
        attributeStr.addAttributes([NSAttributedString.Key.foregroundColor:BASE_GRAY_COLOR, NSAttributedString.Key.font:UIFont.systemFont(ofSize: 15, weight: .medium)], range: NSMakeRange(0, string.count))
        
        let textField = UITextField.init()
        textField.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        textField.textColor = UIColor.hexadecimalColor(hexadecimal: "#3B3B3B")
        textField.attributedPlaceholder = (attributeStr.copy() as! NSAttributedString)
        textField.borderStyle = .none
        textField.clearButtonMode = .whileEditing
        return textField
    }
    
    func drawLine() -> UIView {
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F5F5F5")
        return line
    }
}
